package dp;

import java.util.List;

public class WordBreak139 {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length()];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = false;
        }


        for (int i = 0; i < s.length(); i++) {
            if (wordDict.contains(s.substring(0, i+1))) {
                dp[i] = true;
                continue;
            }
            for (int j = i-1; j >= 0; j--) {
                if (dp[j] && wordDict.contains(s.substring(j+1, i+1))) {
                    dp[i] = true;
                }
            }
        }
        return dp[dp.length-1];
    }
}
